<link rel="stylesheet" href="/plug/layui/css/layui.css">
<div style="margin: 40px;">
    <form class="layui-form" xmlns:th="http://www.thymeleaf.org">
        <input type="hidden" name="id" th:value="${curriculum == null ? '' : curriculum.id}">
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">课程名</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" maxlength="100" th:value="${curriculum == null ? '' : curriculum.name}" placeholder="请输入课程名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-select">
            <label class="layui-form-label">周</label>
            <div class="layui-input-block">
                <select name="week">
                    <option value="1" th:selected="${curriculum != null && curriculum.week == 1}">周一</option>
                    <option value="2" th:selected="${curriculum != null && curriculum.week == 2}">周二</option>
                    <option value="3" th:selected="${curriculum != null && curriculum.week == 3}">周三</option>
                    <option value="4" th:selected="${curriculum != null && curriculum.week == 4}">周四</option>
                    <option value="5" th:selected="${curriculum != null && curriculum.week == 5}">周五</option>
                    <option value="6" th:selected="${curriculum != null && curriculum.week == 6}">周六</option>
                    <option value="0" th:selected="${curriculum != null && curriculum.week == 0}">周日</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">开始时间</label>
            <div class="layui-input-block">
                <input type="text" name="startTime" lay-verify="required" th:value="${curriculum == null ? '' : curriculum.startTime}" readonly placeholder="请输入开始时间" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">结束时间</label>
            <div class="layui-input-block">
                <input type="text" name="endTime" lay-verify="required" th:value="${curriculum == null ? '' : curriculum.endTime}" readonly placeholder="请输入结束时间" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-select">
            <label class="layui-form-label">年级</label>
            <div class="layui-input-block">
                <select name="grade" lay-filter="grade">
                    <option th:each="data: ${gradeList}" th:value="${data.id}" th:selected="${curriculum != null && data.id == curriculum.gId}" th:text="${data.name}"></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-form-select">
            <label class="layui-form-label">班级</label>
            <div class="layui-input-block">
                <select name="gradeClassId" lay-verify="required" lay-filter="class">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <input type="text" name="remarks" maxlength="100" placeholder="请输入备注" th:value="${curriculum == null ? '' : curriculum.remarks}" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="submit">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                <button type="button" onclick="editClose()" class="layui-btn layui-btn-warm">关闭</button>
            </div>
        </div>
    </form>
</div>
<script src="/js/jquery-3.4.1.min.js" type="text/javascript" charset="utf-8"></script>
<script src="/plug/layui/layui.all.js"></script>
<script>
    //加载layui
    layui.use(['layer', 'form', 'laydate'], function(){
        var layer = layui.layer
            ,form = layui.form
            ,date = layui.laydate;

        //layui提交
        form.on('submit(submit)', function(data){
            $.ajax({
                data:JSON.parse(JSON.stringify(data.field)),
                type:"post",
                url:"/curriculum/edit",
                success:function (rest) {
                    layer.msg(rest.msg);
                    if(rest.code === 200) {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        parent.location.reload();
                    }
                },
                error:function () {
                    layer.msg("发生错误，请联系管理员");
                }
            });
            return false;
        });

        date.render({
            elem: 'input[name=startTime]',
            type: 'time',
            format: 'HH:mm:ss'
        })

        date.render({
            elem: 'input[name=endTime]',
            type: 'time',
            format: 'HH:mm:ss'
        })

        form.on('select(grade)', function (data) {
            $.get("/gradeClass/getClassByGrade?id=" + data.value, function (res) {
                if(res.code === 200) {
                    $('select[name=gradeClassId]').empty();
                    $(res.data.list).each(function () {
                        $('select[name=gradeClassId]').append(`<option value="${this.id}" ${this.id === [[${curriculum != null ? curriculum.gradeClassId : -1}]] ? 'selected' : ''}>${this.name}</option>`);
                    })
                    form.render('select');
                }
            })
        })
        $('select[name=grade]').siblings("div.layui-form-select").find('dl dd[lay-value=' + $("select[name=grade]").val() + ']').click();
    });

    function editClose() {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }
</script>